package com.qinglei.recoup.longconn.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.qinglei.recoup.common.domain.CommonConstant;
import com.qinglei.recoup.common.domain.RadarMessage;
import com.qinglei.recoup.common.utils.DateUtil;
import com.qinglei.recoup.common.utils.JsonUtils;
import com.qinglei.recoup.longconn.service.FileService;
import com.qinglei.recoup.longconn.service.IotMessageHandleV7Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Iterator;
import java.util.Map;

/**
 * 物联网消息处理V7协议实现
 *
 * @author yujunwei
 * @datetime 2022-03-04 16:16
 */
@Slf4j
@Service
public class IotMessageHandleV7ServiceImpl implements IotMessageHandleV7Service {

    @Autowired
    private FileService fileService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("${spring.rabbitmq.host:}")
    private String rabbitmqHost;

    @Override
    public Object parseIotMessage(Map<String, Object> map) {
        JsonNode rootNode = JsonUtils.stringToJson(map.get("content").toString());
        JsonNode itemsNode = rootNode.get("items");

        ObjectNode objectNode = JsonUtils.getMAPPER().createObjectNode();
        objectNode.put("SN", map.get("sn").toString());
        /*Instant instant = Instant.ofEpochMilli(rootNode.get("gmtCreate").asLong());
        LocalDateTime time = LocalDateTime.ofInstant(instant, ZoneOffset.of("+8"));
        objectNode.put("TIME", DateUtil.formatFullTime(time, DateUtil.FULL_TIME_MILLI_BAR_PATTERN));*/
        objectNode.put("TIME", rootNode.get("gmtCreate").asLong());
        Iterator<Map.Entry<String, JsonNode>> iterator = itemsNode.fields();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonNode> entry = iterator.next();
            objectNode.set(entry.getKey().split(":")[1], entry.getValue().get("value"));
        }
        Object data = JsonUtils.jsonToBean(objectNode.toString(), Object.class);
        return data;
    }

    @Override
    public void handleIotMessage(Object message) {
        sendIotMessage(message);
        fileService.save(message);
    }

    private void sendIotMessage(Object message) {
        RadarMessage radarMessage = new RadarMessage();
        radarMessage.setType(RadarMessage.TYPE_USER_DATA);
        radarMessage.setProCode("7_0");
        radarMessage.setData(message);
        // 发送消息到MQ
        sendMessage2MQ(radarMessage);
    }

    /**
     * 发送消息到消息队列
     *
     * @param radarMessage
     */
    private void sendMessage2MQ(RadarMessage radarMessage) {
        if (StringUtils.isNotEmpty(rabbitmqHost)) {
            rabbitTemplate.convertAndSend(CommonConstant.MQ_RECOUP_DIRECT_EXCHANGE, CommonConstant.MQ_RADAR_DATA_ROUTING, radarMessage);
        }
    }
}
